package com.wzp.util.source;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhengpeng.wang
 */
public class ConvertSourceUtil {

    public static void main(String[] args) {
        String adapterRequestSource = adapterRequestSource("主站传给辅营的reqSource");
        String mappedSource = sourceMap.getOrDefault(adapterRequestSource, StringUtils.EMPTY);
        String monitorSource = convertRequestSourceForMonitor(mappedSource);
    }

    // reqSource和端的转换关系
    public static final Map<String, String> sourceMap = new HashMap<String, String>() {
        {
            put("mobile.app.android", "andr");
            put("mobile.app.iphone", "ios");
            put("mobile.wap.touch", "touch");
            put("mobile.wap.padtouch", "other");
            put("mobile.app.ipad", "other");
            put("mobile.app.other", "other");
            put("www.domestic", "web");
            put("mobile.app.falcon.iphone", "falIos");
            put("mobile.app.falcon.android", "falAndr");
            put("notNeedX", "notNeedX");
            put("mobile.wap.ptflag.bu", "ald");
            put("mobile.wap.ptflag.amapjp", "gd");
            put("mobile.wap.ptflag.ali", "test");
        }
    };

    // 不需要x商品的source
    static List<String> notNeedXSource =
            Lists.newArrayList("mobile.wap.touch.ccb", "mobile.wap.ptflag.ali", "mobile.wap.ptflag.acbmc");

    // 小程序source
    static List<String> smallProgramTypes = Lists.newArrayList("mobile.wap.ptflag.wx", "mobile.wap.wechat.other");

    /**
     * 适配主站传过来的requestSource,转换成辅营可识别的requestSource
     */
    public static String adapterRequestSource(String reqSource) {
        if (StringUtils.isEmpty(reqSource)) {
            return StringUtils.EMPTY;
        }

        if (notNeedXSource.contains(reqSource)) {
            return "notNeedX";
        }
        // 独立APP兼容（只包含机票售卖）
        if (StringUtils.equals("mobile.app.falcon.android", reqSource)
                || StringUtils.equals("mobile.app.falcon.iphone", reqSource)) {
            return StringUtils.EMPTY;
        }
        for (String smallType : smallProgramTypes) {
            if (StringUtils.contains(reqSource, smallType)) {
                return "mobile.wap.ptflag.bu";
            }
        }
        if ("mobile.wap.ptflag.amapjp".equalsIgnoreCase(reqSource)) {
            return "mobile.wap.ptflag.amapjp";
        }
        if (StringUtils.contains(reqSource, "mobile.wap.ptflag.bu")) {
            return "mobile.wap.ptflag.bu";
        }
        if (StringUtils.contains(reqSource, "iphone")) {
            return "mobile.app.iphone";
        }
        if (StringUtils.contains(reqSource, "android")) {
            return "mobile.app.android";
        }
        if (StringUtils.contains(reqSource, "www")) {
            return "www.domestic";
        }
        if (StringUtils.contains(reqSource, "touch")) {
            return "mobile.wap.touch";
        }
        return "mobile.app.other";
    }

    public static String convertRequestSourceForMonitor(String reqSource) {
        if (StringUtils.contains(reqSource, "mobile.app")) {
            return "app";
        } else if (StringUtils.contains(reqSource, "www") || StringUtils.contains(reqSource, "3w")) {
            return "www";
        } else if (StringUtils.contains(reqSource, "mobile.wap.touch")) {
            return "touch";
        } else if (StringUtils.equals(reqSource, "andr")) {
            return "andr";
        } else if (StringUtils.equals(reqSource, "ios")) {
            return "ios";
        } else if (StringUtils.equals(reqSource, "web")) {
            return "www";
        } else if (StringUtils.equals(reqSource, "touch")) {
            return "touch";
        } else if (StringUtils.equals(reqSource, "fuwu_tgq")) {
            return "tgq";
        } else if (StringUtils.equals(reqSource, "mobile.wap.ptflag.amapjp") || StringUtils.equals(reqSource, "gd")) {
            return "gd";
        } else if (StringUtils.equals(reqSource, "mobile.wap.ptflag.bu") || StringUtils.equals(reqSource, "ald")) {
            return "ald";
        } else {
            return "other";
        }
    }
}
